<?php
class Admin_CategoryController extends Zend_Controller_Action
{
    protected $_category;
    protected $_user;
    protected $_acl;
    protected $_department;

    public function init() {
        $this->_helper->layout()->setLayout('admin');
        $this->_category   = new Admin_Model_Category();
        $this->_acl        = new Application_Model_Acl();
        $this->_department = new Admin_Model_Department();
        $this->_user       = Zend_Auth::getInstance()->getIdentity();
    }
    
    public function indexAction() {
        $availableCategories = $this->_category->getAvailableForRoleCategories($this->_user->roleId);
        $this->view->availableCategories = $availableCategories;        
    }

    public function addCategoryAction() {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();                        

            $categoryId           = $post['category'];            
            $categoryDepartmentId = $this->_category->getCategoryDepartmentId($categoryId);            
            if (!$categoryDepartmentId) {                
                $categoryDepartmentId = isset($post['department'])?$post['department']:0;                
            }            
            //validate importants fields
            $dataForValidate = array(
                'category_name' => $post['category_name'], 
                'alias'         => $post['alias']            
            );                      
            $data = $this->_category->validate(
                $dataForValidate, 
                0,
                $categoryDepartmentId
            );

            if (!isset($data['error'])) {
                $data['parent_id']     = $categoryId;
                $data['department_id'] = $categoryDepartmentId;
                $data['type_id']       = $post['type'];
                $data['description']   = $post['description'];
                $data['keywords']      = $post['keywords'];
                $data['enabled']       = $post['enabled'];
                $data['menu']          = $post['menu'];
                $data['is_link']       = $post['is_link'];
                
                //new category set to the last position by default
                $availableCategories = $this->_acl->getRoleAvailableCategoriesId($this->_user->roleId);
                $maxPosition = $this->_category->getMaxCategoryPosition($availableCategories, $post['category']);
                $data['position'] = $maxPosition?intval($maxPosition)+1:1;                

                $categoryId = $this->_category->addCategory($data);
                $this->_category->allowCategoryForRole($categoryId, $this->_user->roleId);

                My_Cache::clearCache('menu');                
                My_Cache::cleanTagCache(array('dept_menu'));
                $this->_helper->redirector('index', 'category', 'admin');
            } else {
                $this->view->error = $data['error'];                
            }            
        }

        /*$this->view->categories = $this->_category->getAvailableForRoleCategories(
            $this->_user->roleId,
            true
        );*/
        $this->view->categoryTypes = $this->_category->getAvailableForRoleCategoryTypes(
            $this->_user->roleId
        );        
        $this->view->departments = $this->_department->getAvailableForRoleDepartments(
            $this->_user->roleId
        );        
    }

    public function editCategoryAction() {
        $categoryId           = $this->_getParam('category');
        $categoryDepartmentId = $this->_category->getCategoryDepartmentId($categoryId);

        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();                        

            $parentCategoryId     = isset($post['category'])?$post['category']:0;
            $categoryDepartmentId = $this->_category->getCategoryDepartmentId($categoryId);            
            if (!$categoryDepartmentId) {                
                $categoryDepartmentId = isset($post['department'])?$post['department']:0;                
            }

            //validate importants fields
            $dataForValidate = array(
                'category_name' => $post['category_name'],
                'alias'         => $post['alias']
            );          
            $data = $this->_category->validate(
                $dataForValidate, 
                $categoryId, 
                $categoryDepartmentId
            );

            if (!isset($data['error'])) {
                $data['parent_id']     = $parentCategoryId;
                $data['department_id'] = $categoryDepartmentId;
                $data['type_id']       = $post['type'];
                $data['description']   = $post['description'];
                $data['keywords']      = $post['keywords'];
                $data['enabled']       = $post['enabled'];
                $data['menu']          = $post['menu'];
                $data['is_link']       = $post['is_link'];                               

                $categoryId = $this->_category->updateCategory($data, $categoryId);

                My_Cache::clearCache('menu');
                My_Cache::cleanTagCache(array('dept_menu'));
                $this->_helper->redirector('index', 'category', 'admin');
            } else {
                $this->view->error = $data['error'];                
            }            
        }

        $this->view->categoryDepartmentId = $categoryDepartmentId;        
        $this->view->categories = $this->_category->getAvailableForRoleCategories(
            $this->_user->roleId, 
            true
        );
        $this->view->categoryTypes = $this->_category->getAvailableForRoleCategoryTypes(
            $this->_user->roleId
        ); 
        $this->view->category = $this->_category->getCategoryData($categoryId);
        $this->view->departments = $this->_department->getAvailableForRoleDepartments(
            $this->_user->roleId
        );
    }

    public function deleteCategoryAction() {
        $categoryId = $this->_getParam('category');
        $this->_category->deleteCategory($categoryId);

        My_Cache::clearCache('menu');
        My_Cache::cleanTagCache(array('dept_menu'));
        $this->_helper->redirector('index', 'category', 'admin');
    }

    public function changeCategoryPositionAction() {        

    }
}